package com.linhh.mall.center.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linhh.mall.center.api.GoodsInfoService;
import com.linhh.mall.center.common.constant.ConstantKey;
import com.linhh.mall.center.common.pojo.*;
import com.linhh.mall.center.server.mappers.GoodsAttachMapper;
import com.linhh.mall.center.server.mappers.GoodsInfoMapper;
import com.linhh.mall.center.server.mappers.ProductInfoMapper;
import com.linhh.mall.center.server.mappers.ProductIntroduceMapper;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import tk.mybatis.mapper.entity.Example;


import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @Author linhh
 * @Date 2022 12 12 17 31
 **/
@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private ProductIntroduceMapper productIntroduceMapper;

    @Autowired
    private TemplateEngine templateE;

    @Autowired
    private GoodsAttachMapper goodsAttachMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @Override
    public PageInfo<GoodsInfo> queryAllByCondition(GoodsQueryCondition condition) {
        // 设置分页
        PageHelper.startPage(condition.getPage()+1,condition.getSize());
        // 设置组合查询条件
        Example example = new Example(GoodsInfo.class);
        if(condition.getId() != null){
            example.createCriteria().andEqualTo("id",condition.getId());
        }
        if (StringUtils.isNotEmpty(condition.getGoodsName())){
            example.createCriteria().andEqualTo("goodsName",condition.getGoodsName());
        }
        if(condition.getShopId()>0){
            example.createCriteria().andEqualTo("shopId",condition.getShopId());
        }
        if(condition.getStatus() != null){
            example.createCriteria().andEqualTo("status",condition.getStatus());
        }
        List<GoodsInfo> goodsInfoList = goodsInfoMapper.selectByExample(example);
        PageInfo<GoodsInfo> pageInfo = new PageInfo<>(goodsInfoList);
        return pageInfo;
    }

    @Override
    public int addProductInfo(ProductInfo productInfo, String extrAttibute, String introduce) {
        //添加产品
        productInfoMapper.insertUseGeneratedKeys(productInfo);
        //返回产品ID
        Long productId = productInfo.getId();
        //获取产品对列表应商品列表
        List<GoodsInfo> goodList = productInfo.getGoodsList();
        //计算商品数量
        int result = 0;
        for(GoodsInfo goodsInfo:goodList){
            goodsInfo.setProductId(productId);
            goodsInfo.setShopId(productInfo.getShopId());
            goodsInfo.setStatus((byte)1);
            //添加商品对象
            goodsInfoMapper.insertSelective(goodsInfo);
            result++;
        }
        ProductIntroduce productIntroduce = new ProductIntroduce();
        productIntroduce.setProductId(productId);
        ProductIntroduceWithBLOBs productIntroduceWithBLOBs = new
                ProductIntroduceWithBLOBs();
        productIntroduceWithBLOBs.setExtrattribute(extrAttibute);
        productIntroduceWithBLOBs.setIntroduce(introduce);
        //添加产品介绍对象
        productIntroduceMapper.insertSelective(productIntroduce);
        return result;
    }

    @Override
    public String staticPageBuild(GoodsInfo goodsInfo) {
        FileWriter fileWriter = null;
        try{
            fileWriter = new FileWriter("D:/test/hello.html");
        }catch (IOException e){
            e.printStackTrace();
        }
        Context context = new Context();
        context.setVariable("goodsname",goodsInfo.getGoodsName());
        context.setVariable("costprice",goodsInfo.getCostPrice());
        templateE.process("hello",context,fileWriter);
        return "ok";
    }

    @Override
    public int deleteImgByGoodsId(Long goodsId) {
        Example example = new Example(GoodsAttach.class);
        example.createCriteria().andEqualTo("goodsId",goodsId);
        return goodsAttachMapper.deleteByExample(example);
    }

    @Override
    public void relatedImg(GoodsAttach goodsAttach) {
        goodsAttachMapper.insertSelective(goodsAttach);
    }

    @Override
    public List<GoodsAttach> getImgs(Long goodsId) {
        Example example = new Example(GoodsAttach.class);
        example.createCriteria().andEqualTo("goodsId",goodsId);
        return goodsAttachMapper.selectByExample(example);
    }

    @Override
    @DependsOn(value = "rocketMQTemplate")
    public int updateGoods(GoodsInfo goodsInfo) {

        int count = goodsInfoMapper.updateByPrimaryKeySelective(goodsInfo);
        // 更新商品信息，生产者发送商品消息
        rocketMQTemplate.convertAndSend(ConstantKey.TOPIC_NAME,goodsInfo);
        return count;
    }
}
